package com.bjpowernode.controller;

import com.bjpowernode.entity.Economics;
import com.bjpowernode.service.EconomicsService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 处理收支记录相关的请求
 *
 * @author 刘逸晖
 */
@RequestMapping(value = "/myWeb/economics")
public class EconomicsController {

    /**
     * 处理收支记录相关的业务
     */
    private EconomicsService economicsService;

    /**
     * 新增收支记录
     *
     * @param economics 预新增的数据
     * @return 模型和试图
     */
    @RequestMapping(value = "/insert.do")
    public ModelAndView insert(Economics economics) {
        this.economicsService.insertEconomics(economics);

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("info", "收支记录添加成功")
                .setViewName("info");

        return modelAndView;
    }

    /**
     * 查询所有收支记录
     *
     * @return 模型和试图
     */
    @RequestMapping(value = "/find.do")
    public ModelAndView find() {
        List<Economics> economics = this.economicsService.selectAllEconomics();

        ModelAndView modelAndView = new ModelAndView();

        modelAndView.addObject("list", economics)
                .setViewName("economics");

        return modelAndView;
    }

    /**
     * 删除收支记录
     *
     * @param id id
     * @return 收支记录查询页
     */
    @RequestMapping(value = "/delete.do")
    public String delete(Integer id) {
        this.economicsService.deleteEconomicsById(id);

        return "redirect:/myWeb/economics/find.do";
    }

    public EconomicsService getEconomicsService() {
        return economicsService;
    }

    public void setEconomicsService(EconomicsService economicsService) {
        this.economicsService = economicsService;
    }
}
